﻿<!DOCTYPE HTML>
<html lang="zh">
<head>
<title>Context Sensitive Help - Script Example | AutoHotkey v2</title>
<meta name="description" content="The context sensitive help script allows pressing a hotkey in any editor to show the help file page for the selected AutoHotkey function or keyword." />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
</head>
<body>

<h1>Context Sensitive Help in Any Editor -- by Rajat</h1>

<p>This script makes <kbd>Ctrl</kbd>+<kbd>2</kbd> (or another hotkey of your choice) show the help file
 page for the selected AutoHotkey function or keyword. If nothing is selected,
 the function name will be extracted from the beginning of the current line.
</p>
<p><a href="ContextSensitiveHelp.ahk">Download This Script</a> &nbsp;| &nbsp;<a href="index.htm">Other Sample Scripts</a> &nbsp;| &nbsp;<a href="../AutoHotkey.htm">Home</a></p>

<pre class="NoIndent"><em>; The hotkey below uses the clipboard to provide compatibility with the maximum
; number of editors (since ControlGet doesn't work with most advanced editors).
; It restores the original clipboard contents afterward, but as plain text,
; which seems better than nothing.</em>

$^2::
<em>; The following values are in effect only for the duration of this hotkey thread.
; Therefore, there is no need to change them back to their original values
; because that is done automatically when the thread ends:</em>
SetWinDelay 10
SetKeyDelay 0

C_ClipboardPrev := Clipboard
Clipboard := ""
<em>; Use the highlighted word if there is one (since sometimes the user might
; intentionally highlight something that isn't a function):</em>
Send "^c"
if !ClipWait(0.1)
{
    <em>; Get the entire line because editors treat cursor navigation keys differently:</em>
    Send "{home}+{end}^c"
    if !ClipWait(0.2)  <em>; Rare, so no error is reported.</em>
    {
        Clipboard := C_ClipboardPrev
        return
    }
}
C_Cmd := Trim(Clipboard)  <em>; This will trim leading and trailing tabs &amp; spaces.</em>
Clipboard := C_ClipboardPrev  <em>; Restore the original clipboard for the user.</em>
Loop Parse, C_Cmd, "`s"  <em>; The first space is the end of the function.</em>
{
    C_Cmd := A_LoopField
    break <em>; i.e. we only need one interation.</em>
}
if !WinExist("AutoHotkey Help")
{
    <em>; Determine AutoHotkey's location:</em>
    ahk_dir := RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\AutoHotkey", "InstallDir")
    if ErrorLevel  <em>; Not found, so look for it in some other common locations.</em>
    {
        if A_AhkPath
            SplitPath A_AhkPath,, ahk_dir
        else if FileExist("..\..\AutoHotkey.chm")
            ahk_dir := "..\.."
        else if FileExist(A_ProgramFiles "\AutoHotkey\AutoHotkey.chm")
            ahk_dir := A_ProgramFiles "\AutoHotkey"
        else
        {
            MsgBox "Could not find the AutoHotkey folder."
            return
        }
    }
    Run ahk_dir "\AutoHotkey.chm"
    WinWait "AutoHotkey Help"
}
<em>; The above has set the "last found" window which we use below:</em>
WinActivate
WinWaitActive
C_Cmd := StrReplace(C_Cmd, "#", "{#}")
Send "!n{home}+{end}" C_Cmd "{enter}"
return
</pre>
</body>
</html>
